package au.com.mason.propertyTracker.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import au.com.mason.propertyTracker.domain.SaleProperty;
import au.com.mason.propertyTracker.domain.Status;
import au.com.mason.propertyTracker.form.AddPropertyForm;
import au.com.mason.propertyTracker.service.PropertyService;
import au.com.mason.propertyTracker.service.SuburbService;

@Controller
public class AddProperty {
	
	private PropertyService propertyService;
	private SuburbService suburbService;

	@Autowired
	public AddProperty(PropertyService propertyService, SuburbService suburbService) {
		super();
		this.propertyService = propertyService;
		this.suburbService = suburbService;
	}
	
	@RequestMapping("/displayAddProperty.action")
	public String displayAddProperty(HttpSession session, Map model) {
		boolean rental = false;
		if (session.getAttribute("rental") != null) {
			rental = (Boolean) session.getAttribute("rental");
		}
		model.put("suburbs", suburbService.getSuburbs());
		SaleProperty property = null;
		if (rental) {
			property = new SaleProperty();
		}
		AddPropertyForm addPropertyForm = new AddPropertyForm();
		addPropertyForm.setProperty(property);
		
		model.put("addPropertyForm", addPropertyForm);
		model.put("statuses", Status.values());
		
		return "addProperty";
	}
	
	@RequestMapping("/submitAddProperty.action")
	public String submitAddProperty(AddPropertyForm addPropertyForm) {
		propertyService.saveProperty(addPropertyForm.getProperty());
		
		return "redirect:SubmitFilterPropertiesFromSession.action";
	}

}
